#include "playermainpage.h"
#include "./ui_playermainpage.h"
enum stackedWidgetPage
{
    HOMEPAGE = 0,
    MYPAGE = 1,
    SYSPAGE = 2
};

PlayerMainPage::PlayerMainPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::PlayerMainPage)
{
    ui->setupUi(this);
    init();
    initClickEvent();
}

PlayerMainPage::~PlayerMainPage()
{
    delete ui;
}
int countValue=0;
void PlayerMainPage::onSwitchPage(int page_id)
{
    qDebug()<<"page_id"<<page_id<<" count:"<<countValue++;
    ui->stackedWidget->setCurrentIndex(page_id);
    switchBtnStyle(page_id);
}

void PlayerMainPage::resetPageStyle(int page_id)
{
    QList<pageSwitchButton*> list = findChildren<pageSwitchButton*>();
    for(auto&e : list)
    {
        if(e->getPageID()!= page_id)
        {
            e->setTextColor("#999999");
        }
    }
}

void PlayerMainPage::switchBtnStyle(int page_id)
{
    resetPageStyle(page_id);
    if(page_id == HOMEPAGE)
    {
        ui->homePagebtn->setImage(":/images/homePage/shouyexuan.png");
        ui->myPagebtn->setImage(":/images/homePage/wode.png");
        ui->sysPagebtn->setImage(":/images/homePage/admin.png");
    }
    else if(page_id == MYPAGE)
    {
        ui->homePagebtn->setImage(":/images/homePage/shouye.png");
        ui->myPagebtn->setImage(":/images/homePage/wodexuan.png");
        ui->sysPagebtn->setImage(":/images/homePage/admin.png");
    }
    else if(page_id == SYSPAGE)
    {
        ui->homePagebtn->setImage(":/images/homePage/shouye.png");
        ui->myPagebtn->setImage(":/images/homePage/wode.png");
        ui->sysPagebtn->setImage(":/images/homePage/adminxuan.png");
    }
    else
    {
        ui->homePagebtn->setImage(":/images/homePage/shouye.png");
        ui->myPagebtn->setImage(":/images/homePage/wode.png");
        ui->sysPagebtn->setImage(":/images/homePage/admin.png");
        qDebug()<<"未知的page_id";
    }
}



void PlayerMainPage::init()
{
    setWindowFlag(Qt::FramelessWindowHint);
    setWindowIcon(QIcon(":/images/homePage/logo.png"));

    setAttribute(Qt::WA_TranslucentBackground);
    QGraphicsDropShadowEffect * shadow_effect = new QGraphicsDropShadowEffect(this);
    shadow_effect->setColor(Qt::black);
    shadow_effect->setOffset(0,0);
    shadow_effect->setBlurRadius(5);
    ui->playerback->setGraphicsEffect(shadow_effect);

    ui->homePagebtn->setImageAndText(":/images/homePage/shouyexuan.png","首页",HOMEPAGE);
    ui->myPagebtn->setImageAndText(":/images/homePage/wode.png","我的",MYPAGE);
    ui->sysPagebtn->setImageAndText(":/images/homePage/admin.png", "系统管理",SYSPAGE);

    ui->stackedWidget->setCurrentIndex(0);
}



void PlayerMainPage::initClickEvent()
{
    connect(ui->min_button,&QPushButton::clicked,this,&QWidget::showMinimized);
    connect(ui->quit_button,&QPushButton::clicked,this,&QWidget::close);

    connect(ui->homePagebtn,&pageSwitchButton::switchPage,this,[=]{ onSwitchPage(HOMEPAGE); repaint();});
    connect(ui->myPagebtn,&pageSwitchButton::switchPage,this,[=]{ onSwitchPage(MYPAGE);repaint();});
    connect(ui->sysPagebtn,&pageSwitchButton::switchPage,this,[=]{ onSwitchPage(SYSPAGE);repaint();});
}

void PlayerMainPage::mousePressEvent(QMouseEvent *event)
{
    QPoint point = event->position().toPoint();
    if(ui->header->geometry().contains(point))
    {
        if(event->button()==Qt::LeftButton)
        {
            mousePosition = event->globalPosition().toPoint() - this->geometry().topLeft();
            return;
        }
    }
    QWidget::mousePressEvent(event);
}

void PlayerMainPage::mouseMoveEvent(QMouseEvent *event)
{
    QPoint point = event->position().toPoint();
    if(ui->header->geometry().contains(point))
    {
        if(event->buttons()==Qt::LeftButton)
        {
            this->move(event->globalPosition().toPoint()-mousePosition);
            return;
        }
    }
    QWidget::mouseMoveEvent(event);
}



